﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.UserControl;

using System.Data.Linq;
using System.Collections.Generic;
using System.Text;

namespace Hisun.View.Management
{
    public partial class UserManagementSelect : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CreateTree();
            }
        }

        private void CreateTree()
        {
            var MyCenterTree = (from a in db.TABC_JS_CostCenter select a).ToArray();
            this.TreeView_CenterTree.Nodes.Clear();
            MakeMenu(MyCenterTree);


        }


        #region 生成作业中心目录数
        private void MakeMenu(TABC_JS_CostCenter[] menuSource)
        {
            foreach (var node in menuSource)
            {
                // 创建父节点
                if (node.上级作业中心ID == 0)
                {
                    TreeNode newNode = new TreeNode(node.作业中心名称, node.作业中心ID.ToString());
                    // newNode.NavigateUrl = node.页面文件;
                    this.TreeView_CenterTree.Nodes.Add(newNode);
                    GenerateSubMenu(GetSubNodeSource(node, menuSource), newNode, menuSource);
                }
            }
            TreeView_CenterTree.ExpandAll();
        }


        /// <summary>
        /// 产生子节点
        /// </summary>
        /// <param name="childNodes"></param>
        /// <param name="fatherNode"></param>
        /// <param name="menuSource"></param>
        private void GenerateSubMenu(TABC_JS_CostCenter[] childNodes, TreeNode fatherNode, TABC_JS_CostCenter[] menuSource)
        {
            foreach (TABC_JS_CostCenter node in childNodes)
            {
                TreeNode newNode = new TreeNode(node.作业中心名称, node.作业中心ID.ToString());
                // newNode.NavigateUrl = node.页面文件;

                fatherNode.ChildNodes.Add(newNode);

                GenerateSubMenu(GetSubNodeSource(node, menuSource), newNode, menuSource);
            }
        }
        /// <summary>
        /// 产生子节点的数据源
        /// </summary>
        /// <param name="father"></param>
        /// <param name="menuSource"></param>
        /// <returns></returns>
        private TABC_JS_CostCenter[] GetSubNodeSource(TABC_JS_CostCenter father, TABC_JS_CostCenter[] menuSource)
        {
            List<TABC_JS_CostCenter> subNodesSource = new List<TABC_JS_CostCenter>();
            foreach (TABC_JS_CostCenter node in menuSource)
            {
                //if (father.是否末级 == 'T')
                //{
                //    break;
                //}
                //else
                //{
                if (node.上级作业中心ID == father.作业中心ID)
                {
                    subNodesSource.Add(node);
                }
                //}
            }
            return subNodesSource.ToArray();
        }
        #endregion

        protected void TreeView_CenterTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            string CenterInfo = TreeView_CenterTree.SelectedNode.Value.ToString() + "&"
                                        + TreeView_CenterTree.SelectedNode.Text ;





            StringBuilder s = new StringBuilder();
            s.Append("<script language=javascript>" + "\n");
            s.Append("window.returnValue='" + CenterInfo.Trim() + "';" + "\n");
            s.Append("window.close();" + "\n");
            s.Append("</script>");
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            string csname = "ltype";
            if (!cs.IsStartupScriptRegistered(cstype, csname))
                cs.RegisterStartupScript(cstype, csname, s.ToString());
        }
    }



}
